class Solution:
    def singleNonDuplicate(self, nums: list[int]) -> int:
        nums_len = len(nums)
        i, j = 0, nums_len - 1
        while i < j:
            mid = (i + j) >> 1
            if mid == 0 or mid == nums_len or nums[mid] != nums[mid + 1] and nums[mid] != nums[mid - 1]:
                return nums[mid]

            if ((j - i) >> 1) & 1:
                if nums[mid] == nums[mid - 1]:
                    i = mid
                else:
                    j = mid
            else:
                if nums[mid] == nums[mid - 1]:
                    j = mid
                else:
                    i = mid
        return nums[i]
